package org.dromara.system.service.impl.trading;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.system.domain.common.BaseTradingReqDTO;
import org.dromara.system.enums.MachineConstant;
import org.dromara.system.enums.NotificationEnum;
import org.dromara.system.enums.PlatformConcreteEnum;
import org.springframework.beans.factory.InitializingBean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author 王星
 * @date 2024/6/27 下午5:47
 */
@Slf4j
public abstract class AbstractTradingService<T, W extends BaseTradingReqDTO> extends BaseTrading<T, W>
    implements InitializingBean, TradingServiceApi, TradingApi<W> {


    @Override
    public void afterPropertiesSet() {
//        PlatformTypeFactory.register(this);
    }

    @Override
    public Object tradingWaterAssemble(String mapBody, Integer mark) {
        return R.ok();
    }

    @Override
    public Object tradingWaterAssemble(String mapBody) {
        return R.ok();
    }

    @Override
    public Object tradingWaterList(String mapBody) {
        return R.ok();
    }

    @Override
    public Object tradingWaterBody(String type, Integer mark, String mapBody) {
        //获取方法
        PlatformConcreteEnum platformConcreteEnum = PlatformConcreteEnum.findOne(type);
        if (ObjectUtil.isNull(platformConcreteEnum)) {
            return R.fail("请求参数格式错误");
        }
        NotificationEnum notificationEnum = getNotificationEnum();
        try {
            Class<?> aClass = Class.forName(notificationEnum.getPath());
            Method method = Arrays.stream(aClass.getMethods())
                .filter(m -> m.getName().equals(platformConcreteEnum.getType()))
                .findFirst().orElse(null);
            if (ObjectUtil.isNotNull(method)) {
                Object result;
                if (ObjectUtil.isNotNull(mark)) {
                    result = method.invoke(SpringUtil.getBean(notificationEnum.getName()), mapBody, mark);
                } else {
                    result = method.invoke(SpringUtil.getBean(notificationEnum.getName()), mapBody);
                }
                try {
                    if (platformConcreteEnum.getReturnType().equals(MachineConstant.NUMBER.ONE)) {
                        return result;
                    }
                } catch (Exception e) {
                    log.error("解析错误:{}", e.getMessage());
                }
                return null;
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            log.error("反射异常请求方法:{}", e.getTargetException().getMessage());
            return R.fail("系统错误:".concat(
                StrUtil.isNotBlank(e.getTargetException().getMessage())
                    ? e.getTargetException().getMessage() : ""));
        } catch (IllegalArgumentException e) {
            log.error("请求参数异常:{}", e.getMessage());
            return R.fail("请求参数校验异常");
        } catch (Exception e) {
            log.error("异常请求方法:{}", e.getMessage());
            return R.fail("服务内部错误");
        }
        return R.fail("请求错误");
    }
}
